﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO.Ports;
using System.Runtime.Serialization.Formatters.Binary;

namespace ConsoleApplication2
{
    class Program
    {
        public SerialPort serial = new SerialPort("COM6");
        public Data data = new Data();
        public Data load = null;

        static void Main(string[] args)
        {
            new Program();
        }

        public Program()
        {
            Console.WriteLine("Welcome");
            setPortname();
            writeSpecialCommands();
            String s = Console.ReadLine();
            while (true)
            {
                if (s == "end")
                {
                    break;
                }
                else if (s == "help")
                {
                    printHelp();
                }
                else if (s == "save")
                {
                    BinaryFormatter b = new BinaryFormatter();
                    using (System.IO.StreamWriter s2 = new System.IO.StreamWriter("Data.dat", false))
                    {
                        b.Serialize(s2.BaseStream, data);
                    }
                    Console.WriteLine("Saved");

                }
                else if (s == "load")
                {
                    BinaryFormatter b = new BinaryFormatter();
                    using (System.IO.StreamReader s2 = new System.IO.StreamReader("Data.dat", false))
                    {
                        load = (Data)b.Deserialize(s2.BaseStream);
                    }
                    foreach (Status status in load.getData())
                    {
                        Console.WriteLine(status);
                    }

                }
                else
                {
                    serial.WriteLine(s);
                    string hulp = serial.ReadLine();
                    Status s3 = Status.createStatus(hulp);

                    if (s3 != null)
                    {
                        data.add(Status.createStatus(hulp));
                    }
                }
                s = Console.ReadLine();
            }
            serial.Close();
            Environment.Exit(0);
        }


        public void setPortname()
        {

            Console.WriteLine("Enter only the number of the port the RX7 is connected with. (Without COM)");
            serial.PortName = "COM" + Console.ReadLine();
            try
            {
                serial.Open();
            }
            catch
            {
                setPortname();
            }
        }

        public void printHelp()
        {
            Console.WriteLine("RS: Reset the bike");
            Console.WriteLine("OP: Calibrate the bike");
            Console.WriteLine("KI: Receive bike info");
            Console.WriteLine("TR: Receive date and time");
            Console.WriteLine("ID: Receive bike ID");
            Console.WriteLine("RD: Receive bike configuration");
            Console.WriteLine("ST: Receive current bike status");
            Console.WriteLine("VE: Receive bike version number\n");
            Console.WriteLine("PW[value]: Set the resistance of the bike");
            Console.WriteLine("PD[value]: Set the distance you want to cycle");
            Console.WriteLine("PE[value]: Set the energy you want to burn");
            Console.WriteLine("PT[value]: Set the time you want ride the bycicle");
            Console.WriteLine("The time you've already driven the is substracted from the given in time.\n");
            Console.WriteLine("To end our ConsoleApplication, simply type 'End'");
            Console.WriteLine("To view this again, type 'Help'\n");
            writeSpecialCommands();
        }

        public void writeSpecialCommands()
        {
            Console.WriteLine("OK! Type a command and press ENTER\n\n****************************************\nFor a list of the commands, type 'help'\nTo save the information to a file, type 'save'\nTo exit the application, press 'exit'\n****************************************");
        }
    }

    [Serializable]
    class Data
    {
        private List<Status> data = new List<Status>();

        public List<Status> getData()
        {
            return data;
        }

        public void add(Status s)
        {
            data.Add(s);
        }
    }

    [Serializable]
    class Status
    {
        int hartslag { get; set; }
        int RPM { get; set; }
        int snelheid { get; set; }
        int afstand { get; set; }
        int power { get; set; }
        int Kj { get; set; }
        string tijdMinuten { get; set; }

        public Status(int hartslag, int rpm, int snelheid, int afstand, int power, int Kj, string tijdMinuten)
        {
            this.hartslag = hartslag;
            this.RPM = rpm;
            this.snelheid = snelheid;
            this.afstand = afstand;
            this.power = power;
            this.Kj = Kj;
            this.tijdMinuten = tijdMinuten;
        }

        public static Status createStatus(string s)
        {
            string[] list = s.Split(new string[] { "	" }, StringSplitOptions.None);

            if (list.Length != 8)
            {
                return null;
            }
            return new Status(int.Parse(list[0]),
                                int.Parse(list[1]),
                                int.Parse(list[2]),
                                int.Parse(list[3]),
                                int.Parse(list[4]),
                                int.Parse(list[5]),
                                list[6]);
        }

        public override string ToString()
        {
            return "Time: " + tijdMinuten + "  Heartbeat: " + hartslag + "  RPM: " + RPM +
                "  Speed: " + snelheid + "  Distance: " + afstand + "  Power: " + power + "  Kj: " + Kj;
        }
    }
}